<?php namespace Environment;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class EnvironmentSetCommand extends EnvironmentCommand {
	
    protected $name = "environment:set";

    protected $description = "Adds the current host name to the provided environment.";

    
    //$env = $app->detectEnvironment(array(
    //	'local' => array('JP-PC067', 'khanhpn-PC'),
    //));
    public function fire()
    {
        $host        = $this->getHost();
        $config      = $this->getConfig();
        $overwrite   = $this->option("host");
        $environment = $this->argument("environment");

        $this->info($host);
        
        if ( ! isset($config[$environment])) {
            $config[$environment] = [];
        }

        $use = $host;

        // If the host option is provided; it will be added to the list of hosts for the provided environment. 
        // If no host option is provided; it will default to the machine the code is being executed on.
        if ($overwrite) {
            $use = $overwrite;
        }

        if ( ! in_array($use, $config[$environment])) {
            $config[$environment][] = $use;
        }

        $this->setConfig($config);

        $this->line("<info>Added</info> <comment>" . $use . "</comment> <info>to</info> <comment>" . $environment . "</comment> <info>environment.</info>");
    }

    protected function getArguments()
    {
        return [
            ["environment", InputArgument::REQUIRED, "The name of the environment to add the current host name to."]
        ];
    }

    protected function getOptions()
    {
        return [
            ["host", null, InputOption::VALUE_OPTIONAL, "The optional host to add to the environment.", null]
        ];
    }
}